package com.tiger.admin.http.otherUser;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tiger.admin.Db;
import com.tiger.common.constant.SourceOfData;
import com.tiger.db.entity.App;
import com.tiger.db.entity.OtherUser;
import com.tiger.db.entity.SchoolDept;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class OtherUserService {
    private final Db db;

    public OtherUserService(Db db) {
        this.db = db;
    }

    public void importUser(MultipartFile file) {

        List<OtherUser> sheetData = new ArrayList<>();

        Instant now = Instant.now();
        Long time = now.getEpochSecond();

        try (Workbook workbook = new XSSFWorkbook(file.getInputStream())) {
            for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
                Sheet sheet = workbook.getSheetAt(i);
                readSheet(sheet, sheetData, time);
            }

            if(!sheetData.isEmpty()) {
                List<String> ghs = new ArrayList<>();
                List<String> deptNames = new ArrayList<>();
                for(OtherUser user : sheetData) {
                    ghs.add(user.getGh());
                    if(user.getDeptCode() != null && !user.getDeptCode().isEmpty()) {
                        if(!deptNames.contains(user.getDeptCode())) {
                            deptNames.add(user.getDeptCode());
                        }
                    }
                }

                List<OtherUser> userRecords = db.otherUser.list(new LambdaQueryWrapper<OtherUser>().in(OtherUser::getGh, ghs));
                ghs = null;

                if(!userRecords.isEmpty()) {
                    List<String> ghRecrods = userRecords.stream().map(OtherUser::getGh).collect(Collectors.toList());
                    sheetData.removeIf(data -> ghRecrods.contains(data.getGh()));
                }

                if(!sheetData.isEmpty()) {
                    if(!deptNames.isEmpty()) {
                        List<SchoolDept> depts = db.schoolDept.list(new LambdaQueryWrapper<SchoolDept>().in(SchoolDept::getName, deptNames));
                        if(!depts.isEmpty()) {
                            for(OtherUser user : sheetData) {
                                if(user.getDeptCode() != null && !user.getDeptCode().isEmpty()) {
                                    Optional<SchoolDept> dept = depts.stream() // 将List转换为流
                                            .filter(item -> item.getName().equals(user.getDeptCode())).findFirst();
                                    if(dept.isPresent()) {
                                        user.setDeptCode(dept.get().getCode());
                                    }else {
                                        user.setDeptCode(null);
                                    }
                                }
                            }
                        }
                    }


                    db.otherUser.saveBatch(sheetData);
                }
            }



        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private void readSheet(Sheet sheet, List<OtherUser> sheetData, Long time) {
        for (Row row : sheet) {
            if(row.getRowNum() == 0) continue;

            OtherUser user = new OtherUser();
            user.setSource(SourceOfData.IMPORT);
            user.setCreateTime(time);


            Cell cell0 = row.getCell(0);

            if(cell0 != null) {
                user.setGh(cell0.getStringCellValue()); //工号
            }

            if(user.getGh() == null || user.getGh().isEmpty()) {
                continue;
            }

            Cell cell1 = row.getCell(1); //姓名
            if(cell1 != null) {
                user.setName(cell1.getStringCellValue());
            }

            if(user.getName() == null || user.getName().isEmpty()) {
                continue;
            }


            Cell cell2 = row.getCell(2); //性别
            if(cell2 != null) {
                String v = cell2.getStringCellValue();
                if(v != null && !v.isEmpty()) {
                    if(v.equals("男")) {
                        user.setSex(1);
                    }else if(v.equals("女")){
                        user.setSex(2);
                    }
                }
            }

            Cell cell3 = row.getCell(3); //电话
            if(cell3 != null) {
                String v = cell3.getStringCellValue();
                if(v != null && !v.isEmpty()) {
                    user.setTel(v);
                }
            }

            Cell cell4 = row.getCell(4); //部门
            if(cell4 != null) {
                String v = cell4.getStringCellValue();
                if(v != null && !v.isEmpty()) {
                    user.setDeptCode(v);
                }
            }

            Cell cell5 = row.getCell(5); //职位
            if(cell5 != null) {
                String v = cell5.getStringCellValue();
                if(v != null && !v.isEmpty()) {
                    user.setPosition(v);
                }
            }

            Cell cell6 = row.getCell(6); //岗位
            if(cell6 != null) {
                String v = cell6.getStringCellValue();
                if(v != null && !v.isEmpty()) {
                    user.setJobPost(v);
                }
            }


            sheetData.add(user);

        }
    }
}
